package com.attendance.www.hr.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.attendance.www.hr.entitys.SystemConfigure;

public class YearFurloughUtils {
	
	/**
	 * 年假计算方法，结果是天数，保留两位小数
	 */
	public static float getYearFurloughHours(Date firstDate,Date entryDate,Date thruDate,List<SystemConfigure> sysList){
		SystemConfigure systemConfigure=sysList.get(0);
		Integer fiveYear=systemConfigure.getAnnualLeaveFive();
		Integer tenYear=systemConfigure.getAnnualLeaveTen();
		Integer fifteenYear=systemConfigure.getAnnualLeaveFifteen();
		//取得年末时间，查询时间区间段的结束时间的年末时间
		Calendar calendarEnd = Calendar.getInstance();
		calendarEnd.setTime(thruDate);
		calendarEnd.set(Calendar.MONTH, 0);
		calendarEnd.set(Calendar.DAY_OF_MONTH,1);
		calendarEnd.set(Calendar.HOUR_OF_DAY,0);
		calendarEnd.set(Calendar.MINUTE,0);
		calendarEnd.set(Calendar.SECOND,0);
		calendarEnd.set(Calendar.MILLISECOND,0);
		calendarEnd.add(Calendar.YEAR, 1);
		calendarEnd.add(Calendar.MILLISECOND, -1);
		//计算工作年数
		Float years = ((float)(calendarEnd.getTime().getTime()-firstDate.getTime()))/1000/3600/24/365;
		//入职本公司年数
		Float yearsEntry = ((float)(calendarEnd.getTime().getTime()-entryDate.getTime()))/1000/3600/24/365;
		if (yearsEntry<=0) {
			return 0f;
		}
		//可休年假天数
		Float yearRestYear=0f;
		if (years<=1f) {
			yearRestYear=0f;
		}
		if (years>1f) {
			if(years>=1&&years<=10) {//1到10年
				yearRestYear=fiveYear * 1f;
			} else if(years>10&&years<11){//10年到11年
				yearRestYear = (years-10) * tenYear + (11-years) * fiveYear;
			} else if(years>=11&&years<=20){//11年到20年
				yearRestYear = tenYear * 1f;
			} else if(years>20&&years<21){//20年到21年
				yearRestYear = (years-20) * fifteenYear + (21-years) * tenYear;
			} else if(years>=21){//21年以上
				yearRestYear = fifteenYear * 1f;
			}
			//如果工作年数在1到2年之间，需要乘系数
			if (years>1f && years<2f) {
				Float precent = (years - 1);
				if (yearsEntry < precent) {
					precent=yearsEntry;
				}
				yearRestYear=yearRestYear * precent;
			} else {
				//如果入职不满一年，乘以入职年数的百分数
				if (yearsEntry<1f) {
					yearRestYear=yearRestYear * yearsEntry;
				}
			}
			yearRestYear=(float)(Math.round(yearRestYear*100))/100;
		}
		return yearRestYear;
	}

}
